package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.FineClueService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fineclue.pojo.dto.EnterpriseDetailGetDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueCustomerDataDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueGetEnableBuinessDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueGetUseDosageDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineCluePageGetDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueRedisTypeDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueUserSurplusUsageDTO;
import com.xbongbong.pro.fineclue.pojo.dto.TransferClueStatusDTO;
import com.xbongbong.pro.fineclue.pojo.vo.EnterpriseDetailGetVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueGetEnableBuinessVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueMsgVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineCluePageGetVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueUserSurplusUsageVO;
import com.xbongbong.pro.fineclue.pojo.vo.TransferClueStatusVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author: hongxiao
 * @date: 2021-12-28 14:05
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/fineClue")
public class MobileFineClueController {
    @Resource
    private FineClueService fineClueService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    
    /***
     * 工商查询列表查询接口
     *
     * @param enterpriseDetailGetDTO
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-12-28 14:06
     * @since
     * @version
     */
    @RequestMapping(value = "/enterprise/getlist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enterpriseGetList(@RequestBody @Valid EnterpriseDetailGetDTO enterpriseDetailGetDTO, BindingResult br) {
        XbbResponse<EnterpriseDetailGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EnterpriseDetailGetVO enterpriseDetailGetVO = fineClueService.enterpriseGetList(enterpriseDetailGetDTO);
                response = new XbbResponse<>(enterpriseDetailGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * 通过精线索，查询企业详情
     *
     * @param enterpriseDetailGetDTO
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-12-28 14:08
     * @since
     * @version
     */
    @RequestMapping(value = "/enterprise/getInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enterpriseGetInfo(@RequestBody @Valid EnterpriseDetailGetDTO enterpriseDetailGetDTO, BindingResult br) {
        XbbResponse<EnterpriseDetailGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EnterpriseDetailGetVO enterpriseDetailGetVO = fineClueService.enterpriseGetInfo(enterpriseDetailGetDTO);
                response = new XbbResponse<>(enterpriseDetailGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 获取精线索提供的页面
     * @param: fineCluePageGetDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2022/1/20 15:03
     */
    @RequestMapping(value = "/enterprise/getFineCluePage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFineCluePage(@RequestBody @Valid FineCluePageGetDTO fineCluePageGetDTO, BindingResult br){
        XbbResponse<FineCluePageGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            FineCluePageGetVO fineCluePageGetVO = null;
            try {
                fineCluePageGetVO = fineClueService.getFineCluePage(fineCluePageGetDTO);
                response = new XbbResponse<>(fineCluePageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 第三方获取用户剩余用量
     * @param: fineClueUserSurplusUsageDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2022/1/24
     */
    @RequestMapping(value = "/get/userSurplusUsage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userSurplusUsageStatistics(@RequestBody @Valid FineClueUserSurplusUsageDTO fineClueUserSurplusUsageDTO, BindingResult br) {
        FineClueUserSurplusUsageVO fineClueUserSurplusUsageVO;
        XbbResponse<FineClueUserSurplusUsageVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                fineClueUserSurplusUsageVO = fineClueService.getSurplusUsage(fineClueUserSurplusUsageDTO);
                response = new XbbResponse<>(fineClueUserSurplusUsageVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /*** 
     * @description: 获取转线索任务状态
     * @param: transferClueStatusDTO
     * @param: br
     * @return: java.lang.String 
     * @author: zhushuainan
     * @date: 2022/2/17 14:35
     */
    @RequestMapping(value = "/transfer/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getTransferStatus(@RequestBody @Valid TransferClueStatusDTO transferClueStatusDTO, BindingResult br){
        TransferClueStatusVO transferClueStatusVO;
        XbbResponse<TransferClueStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                transferClueStatusVO = fineClueService.getTransferStatus(transferClueStatusDTO);
                response = new XbbResponse<>(transferClueStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 获取当前开启的应用
     * @param: fineClueGetEnableBuinessDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2022/1/24 10:16
     */
    @RequestMapping(value = "/getEnableBusiness", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEnableBusiness(@RequestBody @Valid FineClueGetEnableBuinessDTO fineClueGetEnableBuinessDTO, BindingResult br) {
        XbbResponse<FineClueGetEnableBuinessVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FineClueGetEnableBuinessVO fineClueMsgVO = fineClueService.getEnableBuiness(fineClueGetEnableBuinessDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /*** 
     * @description: 把前端传过来的fromId等参数存到redis中，key是前端生成的 
     * @param: fineClueRedisTypeDTO
     * @param: br 
     * @return: java.lang.String 
     * @author: zhushuainan
     * @date: 2022/1/24 10:41
     */
    @RequestMapping(value = "/set/toCustomerInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setTransitionInfo(@RequestBody @Valid FineClueRedisTypeDTO fineClueRedisTypeDTO, BindingResult br) {
        FineClueMsgVO fineClueMsgVO;
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                fineClueMsgVO = fineClueService.setTransitionInfo(fineClueRedisTypeDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /*** 
     * @description:  精线索转客户或线索
     * @param: clueCustomerDataDTO
     * @param: br 
     * @return: java.lang.String 
     * @author: zhushuainan
     * @date: 2022/1/25
     */
    @RequestMapping(value = "/add/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String transitionCustomerOrClue(@RequestBody @Valid FineClueCustomerDataDTO clueCustomerDataDTO, BindingResult br) {
        FineClueMsgVO fineClueMsgVO;
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(clueCustomerDataDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(clueCustomerDataDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                fineClueMsgVO = fineClueService.transitionCustomerOrClue(clueCustomerDataDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 三方推送消耗用量信息接口
     * @param: fineClueGetUseDosageDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2022/1/24
     */
    @RequestMapping(value = "/getUseDosage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUseDosage(@RequestBody @Valid FineClueGetUseDosageDTO fineClueGetUseDosageDTO, BindingResult br) {
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FineClueMsgVO fineClueMsgVO = fineClueService.getUseDosage(fineClueGetUseDosageDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
